/**
 * 
 */
package com.chacha.framework.dasboard.tools;


import java.util.ArrayList;
import java.util.List;

/**
 * @author rachmat_doni@smltech.co.id
 *
 */
public class DataJsonTree {

	private List<String> props;
	private String rootName;
	private List<DataJsonTree> childs;
	
	public DataJsonTree(String rootName){
		props = new ArrayList<String>();
		this.rootName = rootName;
		childs = new ArrayList<DataJsonTree>();
	}

	public String getRootName() {
		return rootName;
	}

	public void setRootName(String rootName) {
		this.rootName = rootName;
	}

	public void addProps(String name, String value){
		props.add("\"" + name + "\": " + value + ",");
	}
	
	public void addChild(DataJsonTree child){
		childs.add(child);
	}
	
	public List<DataJsonTree> getChilds() {
		return childs;
	}

	public void setChilds(List<DataJsonTree> childs) {
		this.childs = childs;
	}

	public String getStringData(){
		StringBuilder initData = new StringBuilder("{"); 	
		StringBuilder data = new StringBuilder();
		
		for(String prop: props){
			initData.append(prop) ;
		}
		initData.delete(initData.length()-1, initData.length());
		
		if(childs != null && childs.size() != 0){
			data.append(", \"children\": [ ");
			for(DataJsonTree dt: childs){
				data.append(dt.getStringData() +",");
			}
			data.delete(data.length()-1, data.length());
			data.append(" ]");
		}
		
		initData.append(data.toString() + " }");
		
		return initData.toString();
	}
}
